"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogReqHandler = void 0;
const GiveHistoryDao_1 = require("../hall_server/dao/GiveHistoryDao");
const Config_1 = require("../src/config/Config");
const ErrorCode_1 = require("../src/config/ErrorCode");
const ServerConfig_1 = require("../src/config/ServerConfig");
const DateUtil_1 = require("../src/engine/utils/DateUtil");
const Logger_1 = require("../src/engine/utils/Logger");
const Md5Helper_1 = require("../src/engine/utils/Md5Helper");
const WebUtil_1 = require("../src/engine/utils/WebUtil");
const ClubTopDao_1 = require("./dao/ClubTopDao");
const Club_Freeze_HistoryDao_1 = require("./dao/Club_Freeze_HistoryDao");
const Club_HistoryDao_1 = require("./dao/Club_HistoryDao");
const User_Coins_HistoryDao_1 = require("./dao/User_Coins_HistoryDao");
const User_Gems_HistoryDao_1 = require("./dao/User_Gems_HistoryDao");
const User_Meilis_HistoryDao_1 = require("./dao/User_Meilis_HistoryDao");
const User_Spend_DailyDao_1 = require("./dao/User_Spend_DailyDao");
const LogMethodConfig_1 = require("./LogMethodConfig");
const LogTest_1 = require("./LogTest");
const RoomStatisticsDao_1 = require("./dao/RoomStatisticsDao");
const ResponseModel_1 = require("../src/model/ResponseModel");
const request = require('request');
const crypto = require("crypto");
const routerMap = {
    updateServerStatus: "/updateServerStatus",
    get_server_info: "/get_server_info"
};
class LogReqHandler {
    init(app) {
        for (const key in routerMap) {
            app.use(routerMap[key], this.onBaseRoot.bind(this));
        }
        for (const key in LogMethodConfig_1.LogMethodConfig.method_type) {
            app.use(LogMethodConfig_1.LogMethodConfig.method_type[key], this.onBaseRoot.bind(this));
        }
    }
    onBaseRoot(req, resp) {
        try {
            let ip = WebUtil_1.WebUtil.getIpByExpressReq(req);
            Logger_1.Logger.log("onBaseRoot=req=", ip, req.baseUrl);
            if (req.baseUrl == routerMap.updateServerStatus) {
                ServerConfig_1.ServerConfig.initConfig(ServerConfig_1.ServerConfig.nowServerConfig.nowPort);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.club_in_out_history) {
                Club_HistoryDao_1.Club_HistoryDao.insert_in_out_history(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.club_freeze_history) {
                Club_Freeze_HistoryDao_1.Club_Freeze_HistoryDao.insert_freeze_history(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.get_club_in_out_history) {
                this.checkBody(ip, req, resp, this.on_get_club_in_out_history.bind(this));
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.club_top) {
                ClubTopDao_1.ClubTopDao.insertTop(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.get_club_top) {
                this.checkBody(ip, req, resp, this.on_get_club_top.bind(this));
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.get_club_top_one) {
                this.checkBody(ip, req, resp, this.on_get_club_top_one.bind(this));
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.get_club_freeze_history) {
                this.checkBody(ip, req, resp, this.on_get_club_freeze_history.bind(this));
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.insert_give_history) {
                GiveHistoryDao_1.GiveHistoryDao.insert_give_history(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.get_give_history) {
                this.checkBody(ip, req, resp, this.on_get_give_history.bind(this));
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.add_user_spend_daily) {
                User_Spend_DailyDao_1.User_Spend_DailyDao.add_user_spend_daily(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.add_user_gems_history) {
                User_Gems_HistoryDao_1.User_Gems_HistoryDao.add_user_gems_history(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.add_user_coins_history) {
                User_Coins_HistoryDao_1.User_Coins_HistoryDao.add_user_coins_history(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == routerMap.get_server_info) {
                LogTest_1.LogTest.instance.leave();
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.add_user_meilis_history) {
                User_Meilis_HistoryDao_1.User_Meilis_HistoryDao.add_user_meilis_history(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.add_room_statistics) {
                RoomStatisticsDao_1.RoomStatisticsDao.insert(req.body);
                resp.end("success");
            }
            else if (req.baseUrl == LogMethodConfig_1.LogMethodConfig.method_type.get_club_room_statics) {
                this.checkBody(ip, req, resp, this.on_get_club_room_statics.bind(this));
            }
        }
        catch (error) {
            Logger_1.Logger.warn("onBaseRoot error", error);
        }
    }
    checkBody(ip, req, resp, callback) {
        if (req.body) {
            try {
                let jsonObj = JSON.parse(req.body);
                let k = jsonObj["k"];
                let data = jsonObj["data"];
                if (k == Md5Helper_1.Md5Helper.getMd5_encrypt(JSON.stringify(data))) {
                    callback && callback(req, resp, data, ip);
                }
                else {
                    Logger_1.Logger.warn("收到非法Http请求 key", ip, req.body);
                    resp.end();
                }
            }
            catch (error) {
                Logger_1.Logger.warn("收到非法Http请求body", ip, req.body, error);
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }
    on_get_club_in_out_history(req, resp, data, ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let clubid = data["clubid"];
            let result = yield Club_HistoryDao_1.Club_HistoryDao.get_in_out_history(clubid);
            let obj = {
                "code": ErrorCode_1.ErrorCode.Code.Suc,
                "data": result
            };
            resp.end(JSON.stringify(obj));
        });
    }
    on_get_club_freeze_history(req, resp, data, ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let clubid = data["clubid"];
            let result = yield Club_Freeze_HistoryDao_1.Club_Freeze_HistoryDao.get_history(clubid);
            let obj = {
                "code": ErrorCode_1.ErrorCode.Code.Suc,
                "data": result
            };
            resp.end(JSON.stringify(obj));
        });
    }
    on_get_club_top(req, resp, data, ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let clubid = data["clubid"];
            let topType = data["topType"];
            let dateStr = data["dateStr"];
            let page_size = data["page_size"];
            let cur_page = data["cur_page"];
            let result = yield ClubTopDao_1.ClubTopDao.get_club_top(clubid, topType, dateStr, page_size, cur_page);
            let totalNum = yield ClubTopDao_1.ClubTopDao.get_club_top_totalnum(clubid, dateStr);
            let resultObj = {
                "data": result,
                "totalNum": totalNum
            };
            let obj = {
                "code": ErrorCode_1.ErrorCode.Code.Suc,
                "data": resultObj
            };
            resp.end(JSON.stringify(obj));
        });
    }
    on_get_club_top_one(req, resp, data, ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let clubid = data["clubid"];
            let userid = data["userid"];
            let dateStr = data["dateStr"];
            let result = yield ClubTopDao_1.ClubTopDao.get_club_top_one(clubid, userid, dateStr);
            let obj = {
                "code": ErrorCode_1.ErrorCode.Code.Suc,
                "data": result
            };
            resp.end(JSON.stringify(obj));
        });
    }
    on_get_give_history(req, resp, data, ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let sender = data["sender"];
            let reciver = data["reciver"];
            let page_size = data["page_size"];
            let cur_page = data["cur_page"];
            let result = yield GiveHistoryDao_1.GiveHistoryDao.get_give_history(sender, reciver, cur_page, page_size);
            let totalNum = yield GiveHistoryDao_1.GiveHistoryDao.get_give_history_totalnum(sender, reciver);
            let resultObj = {
                "data": result,
                "totalNum": totalNum
            };
            let obj = {
                "code": ErrorCode_1.ErrorCode.Code.Suc,
                "data": resultObj
            };
            resp.end(JSON.stringify(obj));
        });
    }
    on_get_club_room_statics(req, resp, data, ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let clubid = data["clubid"];
            let time = data["time"];
            let startTime = DateUtil_1.default.formatDateStr(time);
            let endTime = DateUtil_1.default.formatDateStr(time + Config_1.Config.OneDay);
            let dataList = yield RoomStatisticsDao_1.RoomStatisticsDao.get_statistics(clubid, startTime, endTime);
            let respModel = new ResponseModel_1.ResponseModel();
            respModel.data = dataList.length;
            resp.end(JSON.stringify(respModel));
        });
    }
}
exports.LogReqHandler = LogReqHandler;
LogReqHandler.instance = new LogReqHandler();
